package com.daxia.ogmall.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.daxia.ogmall.dao.OrderDesignerDAO;
import com.daxia.ogmall.dto.OrderDesignerDTO;
import com.daxia.ogmall.model.OrderDesigner;
import com.daxia.ogmall.support.Page;
import com.daxia.ogmall.util.BeanMapper;

@Service
public class OrderDesignerService {
	
	@Autowired
	private OrderDesignerDAO orderDesignerDAO;
	
	public List<OrderDesignerDTO> find(OrderDesignerDTO query, Page page) {
		List<OrderDesigner> models = orderDesignerDAO.find(query, page);
		List<OrderDesignerDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<OrderDesignerDTO> findAll() {
		return this.find(new OrderDesignerDTO(), null);
	}
	
	public List<OrderDesignerDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new OrderDesignerDTO(), page);
	}
	
	public Long create(OrderDesignerDTO dto) {
		OrderDesigner model = new OrderDesigner();
		toModel(model, dto);
		return orderDesignerDAO.create(model);
	}
	
	public OrderDesignerDTO load(Long id) {
	    OrderDesigner model = orderDesignerDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(OrderDesignerDTO dto) {
		OrderDesigner model = orderDesignerDAO.load(dto.getId());
		toModel(model, dto);
		orderDesignerDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		if (ids != null) {
			for (Long id : ids) {
				orderDesignerDAO.deleteById(id);
			}
		}
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public OrderDesignerDTO findOne(OrderDesignerDTO query) {
		OrderDesigner model = orderDesignerDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<OrderDesignerDTO> toDTOs(List<OrderDesigner> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<OrderDesignerDTO>(0);
		}
		List<OrderDesignerDTO> dtos = new ArrayList<OrderDesignerDTO>(models.size());
		for (OrderDesigner model : models) {
	        OrderDesignerDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private OrderDesignerDTO toDTO(OrderDesigner model) {
		if (model == null) {
			return null;
		}
		OrderDesignerDTO dto = BeanMapper.map(model, OrderDesignerDTO.class);
		
		return dto;
	}
	
	private void toModel(OrderDesigner model, OrderDesignerDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<OrderDesigner> toModels(List<OrderDesignerDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<OrderDesigner>(0);
		}
		List<OrderDesigner> models = new ArrayList<OrderDesigner>(dtos.size());
		for (OrderDesignerDTO dto : dtos) {
	        OrderDesigner model = new OrderDesigner();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
	
	public int updateDesigner(Long id, Long designerId) {
		return orderDesignerDAO.updateDesigner(id, designerId);
	}
	
	public BigInteger countAllOfADay() {
		return orderDesignerDAO.countAllOfADay();
	}
	
	public BigInteger countSuccedOfADay() {
		return orderDesignerDAO.countSuccedOfADay();
	}
}
